﻿using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.MES.Customer.Infrastructure.Repository
{
    /// <summary>
    /// 收入仓储实现层
    /// </summary>
    public class IncomeStatementRepository : BaseRepository<IncomeStatement>, IIncomeStatementRepository
    {
        private readonly ISqlSugarClient _db;
        public IncomeStatementRepository(ISqlSugarClient db) : base(db)
        {
            _db = db;
        }
        /// <summary>
        /// 收入添加
        /// </summary>
        /// <param name="incomeStatement"></param>
        /// <returns></returns>
        public int AddIncomeState(IncomeStatement incomeStatement)
        {
            var result=_db.Insertable(incomeStatement).ExecuteCommand();
            return result;
        }
        ///// <summary>
        ///// 统计金额
        ///// </summary>
        ///// <param name="Flag"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public List<IncomeStatementDTOs> CountIncomeStatement(int Flag)
        //{
        //    return _db.Queryable<IncomeStatement>().ToList().GroupBy(x=>x.IncomeDate.ToString("yyyy-MM-dd").Select(y=>new IncomeStatementDTOs
        //    {
        //         IncomeDate=y.key,
        //         ShowOrderSubject=y.count(),
        //    })
        //}

        /// <summary>
        /// 收入删除
        /// </summary>
        /// <param name="incomeId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<IncomeStatement> DeleteIncomeStatement(int incomeId)
        {
           APIResult<IncomeStatement> apiResult = new APIResult<IncomeStatement>();
            _db.Deleteable<IncomeStatement>(incomeId).ExecuteCommand();
            apiResult.SuccessDel();
            return apiResult;
        }
        /// <summary>
        /// 收入批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<List<IncomeStatement>> DeletesIncomeStatement(List<IncomeStatement> list)
        {
            APIResult<List<IncomeStatement>> apiResult = new APIResult<List<IncomeStatement>>();
            foreach (var incomeStatement in list)
            {
                incomeStatement.IsDelete = true;
            }
            _db.Updateable(list).ExecuteCommand();
            apiResult.SuccessDel();
            return apiResult;
        }
        /// <summary>
        /// 收入编辑
        /// </summary>
        /// <param name="incomeId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IncomeStatement EditIncomeStatement(int incomeId)
        {
            var result = _db.Queryable<IncomeStatement>().InSingle(incomeId);
            return result;
        }

        /// <summary>
        /// 收入显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public PageApiResut<IncomeStatementDTO> GetIncomeStatement(int PageIndex, int PageSize)
        {
            PageApiResut<IncomeStatementDTO> pageApiResut = new PageApiResut<IncomeStatementDTO>();
            int totalCount = _db.Queryable<IncomeStatement>().Where(x => x.IsDelete == false).Count();

            var query = _db.Queryable<IncomeStatement, IncomeType>((A, B) => new JoinQueryInfos(
                JoinType.Inner, A.IncomeTypeId == B.IncomeTypeId
                )).Select((A, B) => new IncomeStatementDTO
                {
                    IncomeTypeId = A.IncomeTypeId,
                    AccountTitle = A.AccountTitle,
                    IncomeDate = A.IncomeDate,
                    incomeId = A.incomeId,
                    IncomePrice = A.IncomePrice,
                    IncomeTheme = A.IncomeTheme,
                    IncomeTypeName = B.IncomeTypeName,
                    NetworkName = A.NetworkName,
                    Payer = A.Payer,
                    ReceiptNumber = A.ReceiptNumber,
                    Remark = A.Remark,
                     BankAccount = A.BankAccount,
                      DepositBank = A.DepositBank,
                    IncomeStatementImage = A.IncomeStatementImage,
                }).OrderByDescending(A => A.incomeId).ToPageList(PageIndex, PageSize);
           
            pageApiResut.TotalCount = totalCount;
                pageApiResut.Data = query.OrderBy(x => x.incomeId).ToList(); // 根据需排序字段进行排序
                            ; // 分页查询
            return pageApiResut;
        }

        public List<IncomeType> GetIncomeType()
        {
            var result = _db.Queryable<IncomeType>().ToList();
            return result;
        }
        /// <summary>
        /// 收入更新
        /// </summary>
        /// <param name="incomeStatement"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int UpdateIncomeStatement(IncomeStatement incomeStatement)
        {
            var result = _db.Updateable(incomeStatement).ExecuteCommand();
            return result;
        }
    }
}
